home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / chrome_100_percent.pak / Unnamed File 000021.txt < prev    next >
Text File  |  2013-04-03  |  5KB  |  151 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. // Custom bindings for the app_window API.
  6.  
  7. var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
  8. var sendRequest = require('sendRequest').sendRequest;
  9. var appWindowNatives = requireNative('app_window');
  10. var forEach = require('utils').forEach;
  11. var GetView = appWindowNatives.GetView;
  12. var OnContextReady = appWindowNatives.OnContextReady;
  13.  
  14. chromeHidden.registerCustomHook('app.window', function(bindingsAPI) {
  15.   var apiFunctions = bindingsAPI.apiFunctions;
  16.   apiFunctions.setCustomCallback('create',
  17.                                  function(name, request, windowParams) {
  18.     var view = null;
  19.     if (windowParams.viewId)
  20.       view = GetView(windowParams.viewId, windowParams.injectTitlebar);
  21.  
  22.     if (!view) {
  23.       // No route to created window. If given a callback, trigger it with an
  24.       // undefined object.
  25.       if (request.callback) {
  26.         request.callback();
  27.         delete request.callback;
  28.       }
  29.       return;
  30.     }
  31.  
  32.     if (windowParams.existingWindow) {
  33.       // Not creating a new window, but activating an existing one, so trigger
  34.       // callback with existing window and don't do anything else.
  35.       if (request.callback) {
  36.         request.callback(view.chrome.app.window.current());
  37.         delete request.callback;
  38.       }
  39.       return;
  40.     }
  41.  
  42.     // Initialize appWindowData in the newly created JS context
  43.     view.chrome.app.window.initializeAppWindow(windowParams);
  44.  
  45.     var callback = request.callback;
  46.     if (callback) {
  47.       delete request.callback;
  48.       if (!view) {
  49.         callback(undefined);
  50.         return;
  51.       }
  52.  
  53.       var willCallback = OnContextReady(windowParams.viewId, function(success) {
  54.         if (success) {
  55.           callback(view.chrome.app.window.current());
  56.         } else {
  57.           callback(undefined);
  58.         }
  59.       });
  60.       if (!willCallback) {
  61.         callback(undefined);
  62.       }
  63.     }
  64.   });
  65.  
  66.   apiFunctions.setHandleRequest('current', function() {
  67.     if (!chromeHidden.currentAppWindow) {
  68.       console.error('chrome.app.window.current() is null -- window not ' +
  69.                     'created with chrome.app.window.create()');
  70.       return null;
  71.     }
  72.     return chromeHidden.currentAppWindow;
  73.   });
  74.  
  75.   chromeHidden.OnAppWindowClosed = function() {
  76.     if (!chromeHidden.currentAppWindow)
  77.       return;
  78.     chromeHidden.currentAppWindow.onClosed.dispatch();
  79.   };
  80.  
  81.   // This is an internal function, but needs to be bound with setHandleRequest
  82.   // because it is called from a different JS context.
  83.   apiFunctions.setHandleRequest('initializeAppWindow', function(params) {
  84.     var AppWindow = function() {};
  85.     forEach(chromeHidden.internalAPIs.app.currentWindowInternal, function(fn) {
  86.       AppWindow.prototype[fn] =
  87.           chromeHidden.internalAPIs.app.currentWindowInternal[fn];
  88.     });
  89.     AppWindow.prototype.moveTo = window.moveTo.bind(window);
  90.     AppWindow.prototype.resizeTo = window.resizeTo.bind(window);
  91.     AppWindow.prototype.contentWindow = window;
  92.     AppWindow.prototype.onClosed = new chrome.Event;
  93.     AppWindow.prototype.close = function() {
  94.       this.contentWindow.close();
  95.     };
  96.     AppWindow.prototype.getBounds = function() {
  97.       var bounds = chromeHidden.appWindowData.bounds;
  98.       return { left: bounds.left, top: bounds.top,
  99.                width: bounds.width, height: bounds.height };
  100.     };
  101.     AppWindow.prototype.isMinimized = function() {
  102.       return chromeHidden.appWindowData.minimized;
  103.     };
  104.     AppWindow.prototype.isMaximized = function() {
  105.       return chromeHidden.appWindowData.maximized;
  106.     };
  107.  
  108.     Object.defineProperty(AppWindow.prototype, 'id', {get: function() {
  109.       return chromeHidden.appWindowData.id;
  110.     }});
  111.  
  112.     chromeHidden.appWindowData = {
  113.       id: params.id || '',
  114.       bounds: { left: params.bounds.left, top: params.bounds.top,
  115.                 width: params.bounds.width, height: params.bounds.height },
  116.       minimized: false,
  117.       maximized: false
  118.     };
  119.     chromeHidden.currentAppWindow = new AppWindow;
  120.   });
  121. });
  122.  
  123. function boundsEqual(bounds1, bounds2) {
  124.   if (!bounds1 || !bounds2)
  125.     return false;
  126.   return (bounds1.left == bounds2.left && bounds1.top == bounds2.top &&
  127.           bounds1.width == bounds2.width && bounds1.height == bounds2.height);
  128. }
  129.  
  130. chromeHidden.updateAppWindowProperties = function(update) {
  131.   if (!chromeHidden.appWindowData)
  132.     return;
  133.   var oldData = chromeHidden.appWindowData;
  134.   update.id = oldData.id;
  135.   chromeHidden.appWindowData = update;
  136.  
  137.   var currentWindow = chromeHidden.currentAppWindow;
  138.  
  139.   if (!boundsEqual(oldData.bounds, update.bounds))
  140.     currentWindow["onBoundsChanged"].dispatch();
  141.  
  142.   if (!oldData.minimized && update.minimized)
  143.     currentWindow["onMinimized"].dispatch();
  144.   if (!oldData.maximized && update.maximized)
  145.     currentWindow["onMaximized"].dispatch();
  146.  
  147.   if ((oldData.minimized && !update.minimized) ||
  148.       (oldData.maximized && !update.maximized))
  149.     currentWindow["onRestored"].dispatch();
  150. };
  151.